//
//  LBNetWork.m
//  app
//
//  Created by LINAICAI on 16/6/3.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBNetWork.h"

NSString *const LBNetWorkDidChangeNotification = @"LBNetWorkDidChangeNotification";
static  LBNetWork *sharedInstance = nil;
@interface LBNetWork()
@property(nonatomic , strong)NSString *netWorkType;
@property(nonatomic , assign)AFNetworkReachabilityStatus status;
@end
@implementation LBNetWork
+ (instancetype)shared{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedInstance = [[self alloc] init];
        //初始化设置
        sharedInstance.status = AFNetworkReachabilityStatusUnknown;
        sharedInstance.netWorkType = @"none";
    });
    return sharedInstance;
}
//实时检测网络类型
- (void)listenNetWorkStatusChangeHander:(void(^)(AFNetworkReachabilityStatus status , LBNetWorkStatusChangeDirection direction))hander {
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusUnknown) {
            sharedInstance.netWorkType = @"none";
            if (sharedInstance.status == AFNetworkReachabilityStatusReachableViaWWAN ||
                sharedInstance.status == AFNetworkReachabilityStatusReachableViaWiFi) {
                hander(status , LBNetWorkStatusChangeDirectionYesToNone);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionYesToNone)}];
            }
            else{
                hander(status , LBNetWorkStatusChangeDirectionNone);
            }
        }
        else if (status == AFNetworkReachabilityStatusNotReachable) {
            sharedInstance.netWorkType = @"none";
            if (sharedInstance.status == AFNetworkReachabilityStatusReachableViaWWAN ||
                sharedInstance.status == AFNetworkReachabilityStatusReachableViaWiFi) {
                hander(status , LBNetWorkStatusChangeDirectionYesToNone);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionYesToNone)}];
            }
            else{
                hander(status , LBNetWorkStatusChangeDirectionNone);
            }
        }
        else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
            sharedInstance.netWorkType = @"2G/3G/4G";
            if (sharedInstance.status == AFNetworkReachabilityStatusNotReachable) {
                hander(status , LBNetWorkStatusChangeDirectionNoneToYes);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionNoneToYes)}];
            }
            else if (sharedInstance.status == AFNetworkReachabilityStatusReachableViaWiFi){
                hander(status , LBNetWorkStatusChangeDirectionYesToYes);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionYesToYes)}];
            }
            else{
                hander(status , LBNetWorkStatusChangeDirectionNone);
            }
        }
        else if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            sharedInstance.netWorkType = @"wifi";
            if (sharedInstance.status == AFNetworkReachabilityStatusNotReachable) {
                hander(status , LBNetWorkStatusChangeDirectionNoneToYes);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionNoneToYes)}];
            }
            else if (sharedInstance.status == AFNetworkReachabilityStatusReachableViaWWAN){
                hander(status , LBNetWorkStatusChangeDirectionYesToYes);
                [[NSNotificationCenter defaultCenter]postNotificationName:LBNetWorkDidChangeNotification object:nil userInfo:@{@"status":@(status),@"direction":@(LBNetWorkStatusChangeDirectionYesToYes)}];
            }
            else{
                hander(status , LBNetWorkStatusChangeDirectionNone);
            }
        }
        //记录当前网络状态，以便下一次对比
        sharedInstance.status = status;

    }];
    [reachabilityManager startMonitoring];

}
@end
